# packages
if (!require("librarian")){
install.packages("librarian")
library(librarian)
}
librarian::shelf(
dplyr, DT, dygraphs, glue,googlesheets4, gstat, here, lubridate, mapview, purrr, readr,
raster, rmapshaper, sf, skimr, stars, stringr, tidyr)
select <- dplyr::select
mapviewOptions(fgb = FALSE)
# paths
dir_data <- switch(
Sys.info()["nodename"],
`Bens-MacBook-Pro.local` = "/Users/bbest/My Drive/projects/calcofi/data",
`Cristinas-MacBook-Pro.local` = "/Volumes/GoogleDrive/.shortcut-targets-by-id/13pWB5x59WSBR0mr9jJjkx7rri9hlUsMv/calcofi/data",
`Bens-MacBook-Air.local` = "/Volumes/GoogleDrive/My Drive/projects/calcofi/data")
# TODO: get Erin's Google Drive path and "nodename")
# bird & mammal counts from CalCOFI cruises
bird_mamm_csv <- file.path(dir_data, "/whales-seabirds-turtles/bird-mammal-census/CalCOFI_bird-mammal-census_observations.csv")
# log of transect datetimes & locations from CalCOFI cruises
transects_csv <- file.path(dir_data, "/whales-seabirds-turtles/bird-mammal-census/CalCOFI_bird-mammal-census_transects.csv")
# behavior codes
codes_bird_mamm_beh_csv <- file.path(dir_data, "/whales-seabirds-turtles/bird-mammal-census/CalCOFI_bird-mammal-census_behaviorcodes.csv")
# species codes
codes_bird_mamm_sp_csv <- file.path(dir_data, "/whales-seabirds-turtles/bird-mammal-census/CalCOFI_bird-mammal-census_allspecieslist.csv")
d_bird_mamm <- read_csv(bird_mamm_csv, guess_max = 1000000)
d_transects <- read_csv(transects_csv, guess_max = 1000000)
codes_bird_mamm_beh <- read_csv(codes_bird_mamm_beh_csv)
codes_bird_mamm_sp <- read_csv(codes_bird_mamm_sp_csv)
# d_bottle <- read_csv(bottle_csv, skip=1, col_names = F, guess_max = 1000000)
# names(d_bottle) <- str_split(
# readLines(bottle_csv, n=1), ",")[[1]] %>%
# str_replace("\xb5", "µ")
#
# names(d_transects) <- stringi::stri_encode("", "UTF-8") %>%
# tolower(names(d_transects)) %>%
# str_replace(" " = "_")
# transform transect lat/lon values to pts
# pt_transects <- d_transects %>%
# sf::st_as_sf(coords = c("Longitude Mid (º)", "Latitude Mid (º)"))
# get example AOI (Channel Islands NMS)
sanctuaries_geo <- "https://github.com/noaa-onms/onmsR/raw/12a87dfd4b90f2e3009ccb4913315fb2df7afddc/data-raw/sanctuaries.geojson"
cinms_ply <- sf::st_read(sanctuaries_geo) %>%
dplyr::filter(nms == "CINMS")
## Reading layer `sanctuaries' from data source
## `https://github.com/noaa-onms/onmsR/raw/12a87dfd4b90f2e3009ccb4913315fb2df7afddc/data-raw/sanctuaries.geojson'
## using driver `GeoJSON'
## Simple feature collection with 16 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -180 ymin: -15.38615 xmax: 180 ymax: 48.50589
## Geodetic CRS: WGS 84
# get AOI geom points as WKT for later use with API
cinms_txt <- sf::st_as_text(cinms_ply$geometry)
# get aoi
aoi = cinms_ply
# GET TRANSECTS AS POINTS
# TODO: make transect lines from Longitude Start and Stop
transects <- d_transects %>%
sf::st_as_sf(coords = c("Longitude Mid (º)", "Latitude Mid (º)"), crs = 4326) %>%
distinct()
bird_mamm_census <- d_bird_mamm %>%
left_join(
d_transects,
by = "GIS key") %>%
left_join(
codes_bird_mamm_beh %>%
select(Behavior, Behavior_Description = Description),
by = "Behavior") %>%
left_join(
codes_bird_mamm_sp %>%
mutate(across(
c("Bird", "LargeBird", "Fish", "Mammal", "Include", "Unidentified"),
as.logical)),
by = "Species")
aoi = cinms_ply
pts <- transects
# mapview(transects) # NOTE: this is prohibitively large to render to html (> 100 MB html)
Note: This figure is a static screenshot from the interactive map, which is prohibitively large to host on Github.
pts %>% mutate(x = st_intersects(pts, aoi))
## Simple feature collection with 60715 features and 19 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -126.4742 ymin: 29.77046 xmax: -117.1813 ymax: 37.84153
## Geodetic CRS: WGS 84
## # A tibble: 60,715 × 20
## `GIS key` Cruise `Transect numb…` `Bin number` Date `Time (sec)`
## * <chr> <chr> <dbl> <dbl> <date> <dbl>
## 1 CAC19870502F1 CAC1987… 1 1 1987-05-02 728
## 2 CAC19870502F10 CAC1987… 2 10 1987-05-02 914.
## 3 CAC19870502F11 CAC1987… 2 11 1987-05-02 922.
## 4 CAC19870502F12 CAC1987… 2 12 1987-05-02 931.
## 5 CAC19870502F13 CAC1987… 2 13 1987-05-02 940.
## 6 CAC19870502F14 CAC1987… 2 14 1987-05-02 949.
## 7 CAC19870502F15 CAC1987… 2 15 1987-05-02 958.
## 8 CAC19870502F16 CAC1987… 2 16 1987-05-02 1007.
## 9 CAC19870502F17 CAC1987… 2 17 1987-05-02 1016.
## 10 CAC19870502F18 CAC1987… 2 18 1987-05-02 1026.
## # … with 60,705 more rows, and 14 more variables: `Latitude Start (º)` <dbl>,
## # `Longitude Start (º)` <dbl>, `Latitude Stop (º)` <dbl>,
## # `Longitude Stop (º)` <dbl>, `Length (m)` <dbl>, `Width (m)` <dbl>,
## # `Area (m²)` <dbl>, `Depth (m)` <dbl>, `Julian date` <dbl>,
## # `Julian day` <dbl>, SVY <chr>, Season <chr>, geometry <POINT [°]>,
## # x <sgbp[,1]>
# find transects within aoi
pts_aoi <- pts %>%
mutate(
x = st_intersects(pts, aoi) %>% as.logical()) %>%
filter(x)
mapview(pts_aoi, color = "yellow", add=T)
# test summary
bird_mamm_summ <- bird_mamm_census %>%
filter(`GIS key` %in% pts_aoi$`GIS key`) %>%
filter(Season %in% c("Spring", "Summer", "Fall", "Winter")) %>%
group_by(Season) %>%
summarize(n_Species = n_distinct(Species))
library(plotly)
plot_ly(
bird_mamm_summ,
x = ~Season, y = ~n_Species, type = "bar",
title = "Number of unique species per season")
See Santora, J. A., and W. J. Sydeman. 2015. Persistence of hotspots and variability of seabird species richness and abundance in the southern California Current. Ecosphere 6(11):214:
Integrating seabirdobservations with station data requires a grid-based approach to resolve spatially explicit timeseries that account for survey effort over time. Allshipboard tracklines, indexed by 3 km intervals(Yen et al. 2006; Fig. 1), was linked to a GIS, as wehave done for other studies in the CCE (Santoraet al. 2011a, b, 2012a, b). The extent of theshipboard trackline (total survey effort) andCalCOFI sampling stations determined the ex-tent and size of grid cells. This was accomplishedusing the create fishnet command in ArcView toproject the individual 3km sampling points ontoa grid with cells size of 0.78 3 0.78 (;4500 km2).The size of cells was chosen to account for totaltrackline effort (Fig. 1) and to reflect the layout ofthe CalCOFI hydrographic and biological sam-pling stations (Fig. 1) in order to permit futureintegration with those data sets. The grid processresolved the location of consistently sampledcells during 1987–2012 with 45 and 48 cells inspring and summer, respectively (Fig. 1).
- (PDF) Persistence of hotspots and variability of seabird species richness and abundance in the southern California Current. Available from: https://www.researchgate.net/publication/283799672_Persistence_of_hotspots_and_variability_of_seabird_species_richness_and_abundance_in_the_southern_California_Current [accessed Mar 31 2022].
We standardized sampling effort by assessingthe number of times the ship visited a cell and theamount of survey effort collected within that cell,relative to all cells in a given season over theentire length of the time series (Santora and Veit2013). To determine a threshold to use as a cutoff, we calculated the mean 6 SD of cell visitsand effort per season, then omitted all effort lessthan 1 SD below the mean.
- (PDF) Persistence of hotspots and variability of seabird species richness and abundance in the southern California Current. Available from: https://www.researchgate.net/publication/283799672_Persistence_of_hotspots_and_variability_of_seabird_species_richness_and_abundance_in_the_southern_California_Current [accessed Mar 31 2022].
pts <- stations
pts_aoi <- pts %>%
mutate(x = st_intersects(pts, aoi) %>% as.logical()) %>%
filter(x)
test_data <- bird_mamm_census %>%
# choices for `date_step`: "day", "week", month", "quarter", "year", "decade"
# get_oceano_var_aoi <- function(
# var, aoi,
# date_step = c("year", "day", "week", "month", "quarter", "decade"),
# depth_min = 0, depth_max = 10){
# test values
# var = "Bottle O2 (ml_L)"; aoi = cinms_ply; date_step = "year"; depth_min = 0; depth_max = 4000
# var = "Salnty"; aoi = cinms_ply; date_step = "year"; depth_min = 0; depth_max = 1000
d <- bird_mamm_census
locations <- bird_mamm_census %>%
select()
pts <- stations
# find stations in aoi
pts_aoi <- pts %>%
mutate(
x = st_intersects(pts, aoi) %>% as.logical()) %>%
filter(x)
d_summ_1 <- d %>%
st_in
d_summ <- d %>% filter(!is.na(.data[[var]]))
d_aoi_summ <- d_summ %>% filter(Sta_ID %in% pts_aoi$Sta_ID)
# d_test <- d %>% filter(!is.na(`Bottle O2(ml_L)`))
# d_test_aoi <- d_test %>% filter(Sta_ID %in% pts_aoi$Sta_ID)
# d_aoi_summ <- d_aoi %>%
# filter(!is.na(.data[[var]]))
empty_data_for_var <- ifelse(nrow(d_aoi_summ) == 0, TRUE, FALSE)
if (empty_data_for_var) {
d_aoi_summ <- d_summ
}
if (any(!is.na(.data[[Depthm]]))) {
d_aoi_summ <- d_aoi_summ %>%
filter(Depthm >= depth_min, Depthm < depth_max)
}
d_aoi_summ <- d_aoi_summ %>%
mutate(Date_Step = update_date(Date, unit = date_step)) %>%
group_by(Date_Step) %>%
summarize(
var_n = n(),
var_min = min(.data[[var]], na.rm = T),
var_q10 = quantile(.data[[var]], probs = 0.10, na.rm = T),
var_avg = mean(.data[[var]], na.rm = T),
var_q90 = quantile(.data[[var]], 0.90, na.rm = T),
var_max = max(.data[[var]], na.rm = T),
var_sd = sd(.data[[var]], na.rm = T)) %>%
rename(Date = Date_Step)
attr(d_aoi_summ, "labels") <- eval(parse(text = glue("var_lookup$`{var}`")))
attr(d_aoi_summ, "date_step") <- date_step
attr(d_aoi_summ, "date_msg") <- glue("This dataset was summarized by {date_step}.")
attr(d_aoi_summ, "aoi") <- ifelse(
empty_data_for_var,
glue("No data were found for {var} in this area of interest. Summaries were conducted across all existing data points."),
glue("Data for {var} in selected area of interest")
)
d_aoi_summ
}